/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import org.apache.log4j.Logger;

import who.tmap.toolkit.impl.MappingToolKitImpl;

/**
 * A class dedicated to manage TMap Designer command line arguments
 * @author stl
 *
 */
public class ArgumentsHelper {
	
    private static Logger logger = Logger.getLogger(ArgumentsHelper.class); 	
	
	public static String DEV_ENV ="/DEVENV";
	public static String NAME ="/NAME";
	public static String PRINT ="/PRINT";
	public static String QUIET ="/QUIET";
	public static String RELOAD ="/RELOAD";	
	
	public static String[] HELP_PARAMS = { "/HELP", "-HELP", "HELP"
										 , "/H", "-H", "H"
										 , "/?",  "-?", "?" };

	private static String[] args;
	
	private static ArgumentsHelper instance =null;
	
	private ArgumentsHelper() {
	}
	
	public static synchronized ArgumentsHelper getInstance() {
		if ( instance == null ) {
			instance =new ArgumentsHelper();
		}
		
		return instance;
	}
	
	public boolean contains( String arg ) {
		boolean contain =false;
		
		if ( args != null ) {
			for( String argStr : args ) {
				if ( argStr.equalsIgnoreCase( arg ) ) {
					contain =true;
					break;
				}
			}
		}
		
		return contain;
	}
	
	/**
	 * Returns values for arguments using values (i.e. name=myfile)
	 * @param arg
	 * @return
	 */
	public String getValue( String arg ) {
		String value =null;
		
		for( int ct =0; ct < args.length; ct++ ) {
			if ( args[ct].equalsIgnoreCase( arg ) ) {
				value =args[ct + 1];
				break;
			}
		}
		
		return value;
	}
	
	public boolean askForHelp() {
		boolean ret =false;
		
		for( String helpParam : HELP_PARAMS ) {
			if ( contains( helpParam ) ) {
				ret =true;
				break;
			}
		}
		
		return ret;
	}
	
	public String[] getArgs() {
		return args;
	}
	
	public void setArgs( String[] args ) {
		ArgumentsHelper.args =args;
	}
	
	public void outputHelpMessage() {
		System.out.println( "TMap Designer " + MappingToolKitImpl.VERSION );
		System.out.println( "command line parameters include:");
		System.out.println( "/name=<value>: value stands for a .tma file to open.");
		System.out.println( "/print: print the thematic map.");
		System.out.println( "/quiet: close TMap Designer after perfoming operations.");		
		System.out.println( "/reload: reload the data from the original data source and update the thematic map.");			
		System.out.println( "/help: outputs this help information.");		
	}
	
}
